<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>阿里博客-帖子-{{ post.title }}</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
        <link rel="shortcut icon" type="image/vnd.microsoft.icon" href="https://avatar.moonshot.cn/avatar/cmri3mecp7fdvr21ch3g" />
</head>
<body>
<h1>{{ post.title }}</h1>
<p class="center">{{ post.content }}</p>
{% with messages = get_flashed_messages() %}
    {% if messages %}
        <ul>
            {% for message in messages %}
                <li>{{ message }}</li>
            {% endfor %}
        </ul>
    {% endif %}
{% endwith %}
<div style="border-style: solid;padding: 10px;margin-top: 20px;">
<small>作者: <a href="{{ url_for('user_posts', user_id=post.author.id) }}">{{ post.author.username }}</a></small>
发帖时间: {{ post.created_at.strftime('%Y-%m-%d %H:%M:%S') }}
{% if post.attachment %}

        <strong>附件:</strong>
        <a href="{{ url_for('static', filename='uploads/' + post.id|string + '/' + post.attachment) }}"
           download>{{ post.attachment }}</a>
        {% if post.attachment.endswith('.jpg') or post.attachment.endswith('.png') or post.attachment.endswith('.bmp') %}
            <img src="{{ url_for('static', filename='uploads/' + post.id|string + '/' + post.attachment) }}"
                 alt="帖子图片" style="max-width: 50px;"/>
        {% endif %}

{% endif %}

<form method="POST" action="{{ url_for('delete_post', post_id=post.id) }}">
    <button type="submit">删除帖子</button>
</form>
<form method="POST" enctype="multipart/form-data">
    <textarea name="content" placeholder="请输入评论内容"></textarea>
    <input type="file" name="attachment">
    <br>
    <button type="submit">发表评论</button>
</form>
</div>

<h4>最新评论:</h4>
<ul>
    {% for comment in post.comments|reverse %}
        <li style="margin-bottom: 5px;display: inline-block;">
            <strong>{{ loop.index }}楼</strong> - <a
                href="{{ url_for('user_posts', user_id=comment.user.id) }}">{{ comment.user.username }}</a>: {{ comment.content }}
            <small>（{{ comment.created_at.strftime('%Y-%m-%d %H:%M:%S') }}）</small>
            {% if comment.attachment %}
                <p>
                    <strong>评论附件:</strong>
                    <a href="{{ url_for('static', filename='uploads/' + post.id|string + '/' + comment.attachment) }}"
                       download>{{ comment.attachment }}</a>
                    {% if comment.attachment.endswith('.jpg') or comment.attachment.endswith('.png') %}
                        <img src="{{ url_for('static', filename='uploads/' + post.id|string + '/' + comment.attachment) }}"
                             alt="评论附件图片" style="max-width: 50px;"/>
                    {% endif %}
                </p>
            {% endif %}
            {% if current_user.is_authenticated and (current_user.is_admin or comment.user.id == current_user.id) %}
                <form method="POST" action="{{ url_for('delete_comment', comment_id=comment.id) }}">
                    <button type="submit">删除评论</button>
                </form>
            {% endif %}
        </li>
    {% endfor %}
    {% if post.comments|length == 0 %}
        <li style="margin-bottom: 5px;display: inline-block;">暂无评论</li>
    {% endif %}
</ul>


<a href="{{ url_for('index') }}">返回首页</a>
</body>
</html>
